package ControlLayer;

import ModelLayer.*;
import DBLayer.*;
import java.sql.*;
import java.util.ArrayList;

public class OrderCtr
{
	public OrderCtr()
	{
		
	}
	
	public ArrayList<Order> findAllOrders()
	{
		IFDBOrder dbOrd = new DBOrder();
		ArrayList<Order> allOrd = new ArrayList<Order>();
		allOrd = dbOrd.getAllOrders(false);
		return allOrd;
	}
	
	public Order findByID(int id)
	{
		IFDBOrder dbOrd = new DBOrder();
		return dbOrd.findOrder(id, true);
	}
	
	public int updateOrder(Customer customer, Date regDate, Date delDate, String status, double totalPrice)
	{
		IFDBOrder dbOrd = new DBOrder();
		Order ord = new Order();
		ord.setCustomer(customer);
		ord.setRegDate(regDate);
		ord.setDeliveryDate(delDate);
		ord.setStatus(status);
		ord.setTotalPrice(totalPrice);
		return dbOrd.updateOrder(ord);
	}
	
	public void insertNew(Customer customer, Date regDate, Date delDate, String status, double totalPrice)
	{
		Order ordObj = new Order();
		ordObj.setCustomer(customer);
		ordObj.setRegDate(regDate);
		ordObj.setDeliveryDate(delDate);
		ordObj.setStatus(status);
		ordObj.setTotalPrice(totalPrice);
		
		try
		{
			DbConnection.startTransaction();
			DBOrder dbOrd = new DBOrder();
			dbOrd.insertOrder(ordObj);
			DbConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DbConnection.rollbackTransaction();
		}
	}
}
